package com.yiren.dbaa.module.project_edit.presenter;

import android.app.Activity;
import android.content.DialogInterface;

import com.google.gson.Gson;
import com.yiren.backstage.currency.entity.LightCombinationEntity;
import com.yiren.backstage.currency.entity.SingleLightEntity;
import com.yiren.backstage.currency.entity.SiteEntity;
import com.yiren.dbaa.module.project_edit.contract.GroupLightContract;
import com.yiren.dbaa.module.project_edit.contract.SingleLightContract;
import com.yiren.dbaa.module.project_edit.model.GroupLightModel;
import com.yiren.dbaa.module.project_edit.model.SingleLightModel;
import com.yiren.dbaa.view.dialog.CustomDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lwb
 * @date : 2022/8/24 10:34
 * @description:  单项灯光P
 */
public class SingleLightPresenter implements SingleLightContract.Presenter {

    private Activity activity;
    private SingleLightContract.Model singleLightModel;
    private GroupLightContract.Model  groupModel;
    private SingleLightContract.View view;
    public SingleLightPresenter(Activity activity) {
        this.activity = activity;
        singleLightModel = new SingleLightModel(activity);
        groupModel = new GroupLightModel(activity);
    }

    @Override
    public void getSingleLightList(SingleLightContract.View view) {
        this.view = view;
        view.loadSingleLightList(singleLightModel.getAllSingleLight());

    }

    @Override
    public void deleteSinglLight(SiteEntity site,SingleLightEntity singleLight) {
        CustomDialog.Builder builder = new CustomDialog.Builder(activity);
        builder.setTitle("温馨提示");
        builder.setMessage("确认删除当前灯光？且组合灯光里该项也会被删除！");
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                String id = singleLight.getId();
                singleLightModel.deleteSingleLight(site,singleLight);
                List<LightCombinationEntity> mGroupLightList =groupModel.getAllLightCombinations();
                for (int i=0;i<mGroupLightList.size();i++){
                    LightCombinationEntity lightCombination = mGroupLightList.get(i);
                    for (int j=0;j<lightCombination.getSingleLightEntities().size();j++){
                        if (lightCombination.getSingleLightEntities().get(j).getId().equals(id)){
                            lightCombination.getSingleLightEntities().remove(j);
                            List<String> ids = new Gson().fromJson(lightCombination.getSingleLightIds(), ArrayList.class);
                            if (ids != null){
                              ids = removeId(ids,String.valueOf(id));
                            }
                            lightCombination.setSingleLightIds(new Gson().toJson(ids));
                            groupModel.updateGroupLight(site,lightCombination);
                        }
                    }
                }
                view.refreshAdapter(singleLightModel.getAllSingleLight());
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }


    public List<String> removeId(List<String> ids, String id) {
        for (int i = 0; i < ids.size(); i++) {
            if (ids.get(i).equals(id)) {
                ids.remove(i);
            }
        }
        return ids;
    }
}
